package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping( "/students")
@RestController
public class StudentController {
@Autowired
private StudentService studentService;

    @GetMapping
    public Result page( StudentQueryParam studentQueryParam){
       PageResult pageResult = studentService.page(studentQueryParam);
       return Result.success(pageResult);
    }


    @LogOperation
    @DeleteMapping( "/{ids}")
    public Result delete( @PathVariable  List <Integer> ids){
        log.info("批量删除学生信息: ids={} ", ids);
       studentService.deleteByIds(ids);
       return Result.success();
    }

    @LogOperation
    @PostMapping
    public Result save(@RequestBody Student student){
       log.info("新增学生信息: {} ", student);
       studentService.save(student);
       return Result.success();
    }

    @LogOperation
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
       log.info("根据id查询学生信息: {} ", id);
       Student student = studentService.getById(id);
       return Result.success(student);
    }

    @LogOperation
    @PutMapping
    public Result update( @RequestBody Student student){
        log.info("修改学生信息: {} ", student);
        studentService.update(student);
       return Result.success();
    }

    @LogOperation
    @PutMapping("/violation/{id}/{score}")
    public Result updateScore(@PathVariable Integer id,@PathVariable Short score){
        log.info("修改学生违纪信息: {} ", id);
        studentService.updateScore(id,score);
        return Result.success();
    }

}
